<?php

namespace app\admin\controller;

use app\model\AddressModel;
use support\Request;
use support\Response;
use Exception;

class AddressController
{
    /**
     * 获取地址列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            $phone = $request->get('phone', '');
            $user_id = $request->get('user_id', '');
            
            $query = AddressModel::query()
                ->orderBy('id', 'desc');
                
            // 按手机号搜索
            if (!empty($phone)) {
                $query->where('phone', 'like', '%' . $phone . '%');
            }
            
            // 按用户ID搜索
            if (!empty($user_id)) {
                $query->where('user_id', $user_id);
            }
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['created_at'] = date('Y-m-d H:i:s', $item->created_at);
                $item['updated_at'] = date('Y-m-d H:i:s', $item->updated_at);
                $item['is_default_text'] = $item->is_default ? '是' : '否';
            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取地址详情
     * @param Request $request
     * @return Response
     */
    public function detail(Request $request): Response
    {
        try {
            $id = $request->get('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $address = AddressModel::find($id);
            if (!$address) {
                throw new Exception('地址不存在');
            }

            $address['created_at'] = date('Y-m-d H:i:s', $address->created_at);
            $address['updated_at'] = date('Y-m-d H:i:s', $address->updated_at);

            return json([
                'data' => $address,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除地址
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = $request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $address = AddressModel::find($id);
            if (!$address) {
                throw new Exception('地址不存在');
            }

            if (!$address->delete()) {
                throw new Exception('删除失败');
            }

            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 